# -*- coding: utf-8 -*-

import sys, os
pth =  os.path.join(os.getcwd(),__file__)
pth = os.path.dirname(pth)
sys.path += [pth[:pth.rfind('/')]]
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import feedparser
from datetime import datetime
from portal.models import Subscription, Post, Package

def read_blogs():
    subs = Subscription.objects.filter(active=True)
    #print dir(Subscription)
    for sub in subs:
        url = sub.url
        d = feedparser.parse(url)
        for item in d['entries']:
            date = datetime(*item.updated_parsed[:7])
            e, created = Post.objects.get_or_create(title=item.title, url=item.link, author=item.author,date=date,content=item.description)
            if created:
                print repr(e)

def read_packages():
	d = feedparser.parse("http://www.python.org/pypi?:action=rss")
	entries = d.entries
	for item in entries:
	    date = datetime(*item.updated_parsed[:7])
	    e, created = Package.objects.get_or_create(title=item.title, url=item.link, date=date,content=item.description)
        if created:
            print repr(e)
            
if __name__ == '__main__':
    read_blogs()
    read_packages()


